package com.hm.health.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hm.health.R;
import com.hm.health.entity.NavigationBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chenshi on 2019/12/20.
 */

public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.NavigationViewHolder> {
    private Context mContext;
    private List<NavigationBean> mNavigationBeans = new ArrayList<>();

    public NavigationAdapter(Context context, List<NavigationBean> navigationBeans) {
        mContext = context;
        mNavigationBeans = navigationBeans;
    }

    @Override
    public NavigationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_navigation, null);
        NavigationViewHolder holder = new NavigationViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(NavigationViewHolder holder, int position) {
        if (null == mNavigationBeans || position > mNavigationBeans.size()) {
            return;
        }

        NavigationBean data = mNavigationBeans.get(position);
        if (data.getImgResIdLeft() > 0) {
            holder.mImgLeft.setImageResource(data.getImgResIdLeft());
        } else {
            holder.mImgLeft.setImageResource(R.mipmap.abc_btn_rating_star_off_mtrl_alpha);
        }
        holder.mTvTitle.setText(data.getTitle());

        if (null != data.getDescription()) {
            holder.mTvDescription.setText(data.getDescription());
        } else {
            holder.mTvDescription.setText("");
        }

        holder.mRootLayout.setOnClickListener(view -> {
            if (null == data.getCallback()) {
                mContext.startActivity(data.getIntent());
            } else {
                data.getCallback().onItemClicked(data);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (null != mNavigationBeans) {
            return mNavigationBeans.size();
        } else {
            return 0;
        }
    }

    public static class NavigationViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_left)
        ImageView mImgLeft;
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.img_right)
        ImageView mImgRight;
        @BindView(R.id.rl_root)
        RelativeLayout mRootLayout;
        @BindView(R.id.tv_description)
        TextView mTvDescription;


        public NavigationViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
